﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;using DAL;
using System.Data.Linq;
namespace BAL
{
    public class BAL_HoaDon
    {
        public QuanLyPhongTroDBDataContext dbs;
        public BAL_HoaDon()
        {
            dbs = new QuanLyPhongTroDBDataContext();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                if (dbs != null)
                {
                    dbs.Dispose();
                    dbs = null;
                }
        }
        public List<DanhSachHoaDonResult> LayDSHoaDon()
        {
            return dbs.DanhSachHoaDon().ToList();
        }public List<Phong> LayPhong()
        {return dbs.Phongs.ToList();
        }

        public void ThemHoaDon(string maHoaDon, string maPhong, DateTime ngayLap,int soDien,int soNuoc)
        {
            HoaDon _hoaDon=new HoaDon(){MaHoaDon = maHoaDon,MaPhong = maPhong,NgayLap = ngayLap};
            
            SuDungDichVu _sdDien=new SuDungDichVu(){MaHoaDon = maHoaDon,MaDichVu = "01",SoLuongSD = soDien};
            SuDungDichVu _sdNuoc = new SuDungDichVu() { MaHoaDon = maHoaDon, MaDichVu = "02", SoLuongSD = soNuoc };

            

            dbs.SuDungDichVus.InsertOnSubmit(_sdDien);
            dbs.SuDungDichVus.InsertOnSubmit(_sdNuoc);
            dbs.HoaDons.InsertOnSubmit(_hoaDon);
            dbs.SubmitChanges();
            
        }
        public void XoaHoaDon(string maHoaDon)
        {

             HoaDon _hoaDon = dbs.HoaDons.Single(hd => hd.MaHoaDon == maHoaDon);
             
           
            XoaDienNuocPhieuThu(maHoaDon);dbs.HoaDons.DeleteOnSubmit(_hoaDon);
            dbs.SubmitChanges();
        }
        public  void XoaDienNuocPhieuThu(string maHoaDon)
        {
            SuDungDichVu _sdDien=dbs.SuDungDichVus.Single(sdDien=>sdDien.MaHoaDon == maHoaDon && sdDien.MaDichVu=="01");
            SuDungDichVu _sdNuoc = dbs.SuDungDichVus.Single(sdDien => sdDien.MaHoaDon == maHoaDon && sdDien.MaDichVu == "02");

            var _phieuThu = from phieuThu in dbs.PhieuThus
                            where phieuThu.MaHoaDon == maHoaDon
                            select phieuThu;
            foreach (var phieuThu in _phieuThu)
            {
                dbs.PhieuThus.DeleteOnSubmit(phieuThu);
                dbs.SubmitChanges();
            }

            dbs.SuDungDichVus.DeleteOnSubmit(_sdDien);
            dbs.SuDungDichVus.DeleteOnSubmit(_sdNuoc);
            
        }
        public void SuaHoaDon(string maHoaDon, string maPhong, DateTime ngayLap,int soDien,int soNuoc)
        {
            HoaDon _hoaDon = dbs.HoaDons.Single(hd => hd.MaHoaDon == maHoaDon);
            _hoaDon.MaPhong = maPhong;
            _hoaDon.NgayLap = ngayLap;

            SuDungDichVu _sdDien=dbs.SuDungDichVus.Single(sdDien=>sdDien.MaHoaDon==maHoaDon && sdDien.MaDichVu=="01");
            _sdDien.SoLuongSD = soDien;
            SuDungDichVu _sdNuoc = dbs.SuDungDichVus.Single(sdDien => sdDien.MaHoaDon == maHoaDon && sdDien.MaDichVu == "02");
            _sdNuoc.SoLuongSD = soNuoc;
            dbs.SubmitChanges();
        }

        public  int SoDienCu(string maPhong,DateTime ngayLap)
        {
            var hoadon = from
                             hd in dbs.HoaDons
                         join sddv in dbs.SuDungDichVus on hd.MaHoaDon equals sddv.MaHoaDon
                         select new { hd.MaHoaDon, hd.MaPhong, hd.NgayLap, sddv.SoLuongSD, sddv.MaDichVu };

            var hoadon1 = from VAR in hoadon
                          where VAR.MaDichVu == "01"
                          select VAR;
            var hoadon2 = from VAR in hoadon1
                          where VAR.MaPhong == maPhong && VAR.NgayLap < ngayLap 
                          select new {VAR.MaHoaDon,VAR.NgayLap,VAR.MaDichVu,VAR.SoLuongSD};
            var hoadon3 = from VAR in hoadon2
                          where VAR.NgayLap == hoadon2.Max(pt => pt.NgayLap)
                          select VAR;
            try
            {
                return hoadon3.First().SoLuongSD;
            }
            catch (Exception)
            {

                return 0;
            }
           

        }
    }
}
